1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 import java.io.*;
32 import java.net.*;
33 import java.nio.*;
34 import java.nio.channels.*;
35 import java.nio.channels.spi.SelectorProvider;
36 import java.nio.file.StandardOpenOption;
37 import java.nio.file.FileAlreadyExistsException;
38 import java.util.Random;
39
40
41 public class Transfer {
42
43 private static Random generator = new Random();
44
45 private static int[] testSizes = {
46 0, 10, 1023, 1024, 1025, 2047, 2048, 2049 };
47
48 public static void main(String[] args) throws Exception {
49 testFileChannel();
50 for (int i=0; i<testSizes.length; i++)
51 testReadableByteChannel(testSizes[i]);
52 xferTest02();
53 xferTest03();
54 xferTest04();
55 xferTest05();
56 xferTest06();
57 xferTest07();
58 xferTest08();
59 xferTest09();
60 }
61
62 private static void testFileChannel() throws Exception {
63 File source = File.createTempFile("source", null);
64 source.deleteOnExit();
65 File sink = File.createTempFile("sink", null);
66 sink.deleteOnExit();
67
68 FileOutputStream fos = new FileOutputStream(source);
69 FileChannel sourceChannel = fos.getChannel();
70 sourceChannel.write(ByteBuffer.wrap(
71 "Use the source, Luke!".getBytes()));
72 sourceChannel.close();
73
74 FileInputStream fis = new FileInputStream(source);
75 sourceChannel = fis.getChannel();
76
77 RandomAccessFile raf = new RandomAccessFile(sink, "rw");
78 FileChannel sinkChannel = raf.getChannel();
79 long oldSinkPosition = sinkChannel.position();
80 long oldSourcePosition = sourceChannel.position();
81
82 long bytesWritten = sinkChannel.transferFrom(sourceChannel, 0, 10);
83 if (bytesWritten != 10)
84 throw new RuntimeException("Transfer failed");
85
86 if (sourceChannel.position() == oldSourcePosition)
87 throw new RuntimeException("Source position didn't change");
88
89 if (sinkChannel.position() != oldSinkPosition)
90 throw new RuntimeException("Sink position changed");
91
92 if (sinkChannel.size() != 10)
93 throw new RuntimeException("Unexpected sink size");
94
95 bytesWritten = sinkChannel.transferFrom(sourceChannel, 1000, 10);
96
97 if (bytesWritten > 0)
98 throw new RuntimeException("Wrote past file size");
99
100 sourceChannel.close();
101 sinkChannel.close();
102
103 source.delete();
104 sink.delete();
105 }
106
107 private static void testReadableByteChannel(int size) throws Exception {
108 SelectorProvider sp = SelectorProvider.provider();
109 Pipe p = sp.openPipe();
110 Pipe.SinkChannel sink = p.sink();
111 Pipe.SourceChannel source = p.source();
112 sink.configureBlocking(false);
113
114 ByteBuffer outgoingdata = ByteBuffer.allocateDirect(size + 10);
115 byte[] someBytes = new byte[size + 10];
116 generator.nextBytes(someBytes);
117 outgoingdata.put(someBytes);
118 outgoingdata.flip();
119
120 int totalWritten = 0;
121 while (totalWritten < size + 10) {
122 int written = sink.write(outgoingdata);
123 if (written < 0)
124 throw new Exception("Write failed");
125 totalWritten += written;
126 }
127
128 File f = File.createTempFile("blah"+size, null);
129 f.deleteOnExit();
130 RandomAccessFile raf = new RandomAccessFile(f, "rw");
131 FileChannel fc = raf.getChannel();
132 long oldPosition = fc.position();
133
134 long bytesWritten = fc.transferFrom(source, 0, size);
135 fc.force(true);
136 if (bytesWritten != size)
137 throw new RuntimeException("Transfer failed");
138
139 if (fc.position() != oldPosition)
140 throw new RuntimeException("Position changed");
141
142 if (fc.size() != size)
143 throw new RuntimeException("Unexpected sink size "+ fc.size());
144
145 fc.close();
146 sink.close();
147 source.close();
148
149 f.delete();
150 }
151
152 public static void xferTest02() throws Exception {
153 byte[] srcData = new byte[5000];
154 for (int i=0; i<5000; i++)
155 srcData[i] = (byte)generator.nextInt();
156
157
158 File source = File.createTempFile("source", null);
159 source.deleteOnExit();
160 RandomAccessFile raf1 = new RandomAccessFile(source, "rw");
161 FileChannel fc1 = raf1.getChannel();
162
163
164 long bytesWritten = 0;
165 while (bytesWritten < 5000) {
166 bytesWritten = fc1.write(ByteBuffer.wrap(srcData));
167 }
168
169
170 File dest = File.createTempFile("dest", null);
171 dest.deleteOnExit();
172 RandomAccessFile raf2 = new RandomAccessFile(dest, "rw");
173 FileChannel fc2 = raf2.getChannel();
174
175 int bytesToWrite = 3000;
176 int startPosition = 1000;
177
178 bytesWritten = fc1.transferTo(startPosition, bytesToWrite, fc2);
179
180 fc1.close();
181 fc2.close();
182 raf1.close();
183 raf2.close();
184
185 source.delete();
186 dest.delete();
187 }
188
189 public static void xferTest03() throws Exception {
190 byte[] srcData = new byte[] {1,2,3,4} ;
191
192
193 File source = File.createTempFile("source", null);
194 source.deleteOnExit();
195 RandomAccessFile raf1 = new RandomAccessFile(source, "rw");
196 FileChannel fc1 = raf1.getChannel();
197 fc1.truncate(0);
198
199
200 int bytesWritten = 0;
201 while (bytesWritten < 4) {
202 bytesWritten = fc1.write(ByteBuffer.wrap(srcData));
203 }
204
205
206 File dest = File.createTempFile("dest", null);
207 dest.deleteOnExit();
208 RandomAccessFile raf2 = new RandomAccessFile(dest, "rw");
209 FileChannel fc2 = raf2.getChannel();
210 fc2.truncate(0);
211
212 fc1.transferTo(0, srcData.length + 1, fc2);
213
214 if (fc2.size() > 4)
215 throw new Exception("xferTest03 failed");
216
217 fc1.close();
218 fc2.close();
219 raf1.close();
220 raf2.close();
221
222 source.delete();
223 dest.delete();
224 }
225
226
227 public static void xferTest04() throws Exception {
228
229
230 String osName = System.getProperty("os.name");
231 if (!osName.startsWith("SunOS"))
232 return;
233
234 File source = File.createTempFile("blah", null);
235 source.deleteOnExit();
236 long testSize = ((long)Integer.MAX_VALUE) * 2;
237 initTestFile(source, 10);
238 RandomAccessFile raf = new RandomAccessFile(source, "rw");
239 FileChannel fc = raf.getChannel();
240 fc.write(ByteBuffer.wrap("Use the source!".getBytes()), testSize - 40);
241 fc.close();
242 raf.close();
243
244 File sink = File.createTempFile("sink", null);
245 sink.deleteOnExit();
246
247 FileInputStream fis = new FileInputStream(source);
248 FileChannel sourceChannel = fis.getChannel();
249
250 raf = new RandomAccessFile(sink, "rw");
251 FileChannel sinkChannel = raf.getChannel();
252
253 long bytesWritten = sourceChannel.transferTo(testSize -40, 10,
254 sinkChannel);
255 if (bytesWritten != 10) {
256 throw new RuntimeException("Transfer test 4 failed " +
257 bytesWritten);
258 }
259 sourceChannel.close();
260 sinkChannel.close();
261
262 source.delete();
263 sink.delete();
264 }
265
266
267 public static void xferTest05() throws Exception {
268
269 File source = File.createTempFile("blech", null);
270 source.deleteOnExit();
271 initTestFile(source, 100);
272
273
274 File sink = null;
275 FileChannel fc = null;
276 while (fc == null) {
277 sink = File.createTempFile("sink", null);
278
279 sink.delete();
280 try {
281 fc = FileChannel.open(sink.toPath(),
282 StandardOpenOption.CREATE_NEW,
283 StandardOpenOption.WRITE,
284 StandardOpenOption.SPARSE);
285 } catch (FileAlreadyExistsException ignore) {
286
287 }
288 }
289 sink.deleteOnExit();
290
291 long testSize = ((long)Integer.MAX_VALUE) * 2;
292 try {
293 fc.write(ByteBuffer.wrap("Use the source!".getBytes()),
294 testSize - 40);
295 } catch (IOException e) {
296
297 System.err.println("xferTest05 was aborted.");
298 return;
299 } finally {
300 fc.close();
301 }
302
303
304 FileChannel sourceChannel = new FileInputStream(source).getChannel();
305 try {
306 FileChannel sinkChannel = new RandomAccessFile(sink, "rw").getChannel();
307 try {
308 long bytesWritten = sinkChannel.transferFrom(sourceChannel,
309 testSize - 40, 10);
310 if (bytesWritten != 10) {
311 throw new RuntimeException("Transfer test 5 failed " +
312 bytesWritten);
313 }
314 } finally {
315 sinkChannel.close();
316 }
317 } finally {
318 sourceChannel.close();
319 }
320
321 source.delete();
322 sink.delete();
323 }
324
325 static void checkFileData(File file, String expected) throws Exception {
326 FileInputStream fis = new FileInputStream(file);
327 Reader r = new BufferedReader(new InputStreamReader(fis, "ASCII"));
328 StringBuilder sb = new StringBuilder();
329 int c;
330 while ((c = r.read()) != -1)
331 sb.append((char)c);
332 String contents = sb.toString();
333 if (! contents.equals(expected))
334 throw new Exception("expected: " + expected
335 + ", got: " + contents);
336 r.close();
337 }
338
339
340 public static void xferTest06() throws Exception {
341 String data = "Use the source, Luke!";
342
343 File source = File.createTempFile("source", null);
344 source.deleteOnExit();
345 File sink = File.createTempFile("sink", null);
346 sink.deleteOnExit();
347
348 FileOutputStream fos = new FileOutputStream(source);
349 fos.write(data.getBytes("ASCII"));
350 fos.close();
351
352 FileChannel sourceChannel =
353 new RandomAccessFile(source, "rw").getChannel();
354 sourceChannel.position(7);
355 long remaining = sourceChannel.size() - sourceChannel.position();
356 FileChannel sinkChannel =
357 new RandomAccessFile(sink, "rw").getChannel();
358 long n = sinkChannel.transferFrom(sourceChannel, 0L,
359 sourceChannel.size());
360 if (n != remaining)
361 throw new Exception("n == " + n + ", remaining == " + remaining);
362
363 sinkChannel.close();
364 sourceChannel.close();
365
366 checkFileData(source, data);
367 checkFileData(sink, data.substring(7,data.length()));
368
369 source.delete();
370 }
371
372
373 public static void xferTest07() throws Exception {
374 File source = File.createTempFile("source", null);
375 source.deleteOnExit();
376
377 FileChannel sourceChannel = new RandomAccessFile(source, "rw")
378 .getChannel();
379 sourceChannel.position(32000L)
380 .write(ByteBuffer.wrap("The End".getBytes()));
381
382
383 ServerSocketChannel ssc = ServerSocketChannel.open();
384 ssc.socket().bind(new InetSocketAddress(0));
385 InetSocketAddress sa = new InetSocketAddress(
386 InetAddress.getLocalHost(), ssc.socket().getLocalPort());
387 SocketChannel sink = SocketChannel.open(sa);
388 sink.configureBlocking(false);
389 SocketChannel other = ssc.accept();
390
391 long size = sourceChannel.size();
392
393
394 long n;
395 do {
396 n = sourceChannel.transferTo(0, size, sink);
397 } while (n > 0);
398
399 sourceChannel.close();
400 sink.close();
401 other.close();
402 ssc.close();
403 source.delete();
404 }
405
406
407
408 public static void xferTest08() throws Exception {
409
410 String osName = System.getProperty("os.name");
411 if (osName.startsWith("Windows"))
412 return;
413
414 final long G = 1024L * 1024L * 1024L;
415
416
417
418 File file = File.createTempFile("source", null);
419 file.deleteOnExit();
420
421 RandomAccessFile raf = new RandomAccessFile(file, "rw");
422 FileChannel fc = raf.getChannel();
423
424 try {
425 fc.write(ByteBuffer.wrap("0123456789012345".getBytes("UTF-8")), 6*G);
426 } catch (IOException x) {
427 System.err.println("Unable to create test file:" + x);
428 fc.close();
429 return;
430 }
431
432
433
434 ServerSocketChannel ssc = ServerSocketChannel.open();
435 ssc.socket().bind(new InetSocketAddress(0));
436
437 InetAddress lh = InetAddress.getLocalHost();
438 InetSocketAddress isa = new InetSocketAddress(lh, ssc.socket().getLocalPort());
439 SocketChannel source = SocketChannel.open(isa);
440 SocketChannel sink = ssc.accept();
441
442 Thread thr = new Thread(new EchoServer(sink));
443 thr.start();
444
445
446
447 long testdata[][] = {
448 { 2*G-1, 1 },
449 { 2*G-1, 10 },
450 { 2*G, 1 },
451 { 2*G, 10 },
452 { 2*G+1, 1 },
453 { 4*G-1, 1 },
454 { 4*G-1, 10 },
455 { 4*G, 1 },
456 { 4*G, 10 },
457 { 4*G+1, 1 },
458 { 5*G-1, 1 },
459 { 5*G-1, 10 },
460 { 5*G, 1 },
461 { 5*G, 10 },
462 { 5*G+1, 1 },
463 { 6*G, 1 },
464 };
465
466 ByteBuffer sendbuf = ByteBuffer.allocateDirect(100);
467 ByteBuffer readbuf = ByteBuffer.allocateDirect(100);
468
469 try {
470 byte value = 0;
471 for (int i=0; i<testdata.length; i++) {
472 long position = testdata[(int)i][0];
473 long count = testdata[(int)i][1];
474
475
476 for (long j=0; j<count; j++) {
477 sendbuf.put(++value);
478 }
479 sendbuf.flip();
480
481
482 fc.write(sendbuf, position);
483 fc.transferTo(position, count, source);
484
485
486 long nread = 0;
487 while (nread < count) {
488 int n = source.read(readbuf);
489 if (n < 0)
490 throw new RuntimeException("Premature EOF!");
491 nread += n;
492 }
493
494
495 readbuf.flip();
496 sendbuf.flip();
497 if (!readbuf.equals(sendbuf))
498 throw new RuntimeException("Echo'ed bytes do not match!");
499 readbuf.clear();
500 sendbuf.clear();
501 }
502 } finally {
503 source.close();
504 ssc.close();
505 fc.close();
506 file.delete();
507 }
508 }
509
510
511
512 static void xferTest09() throws Exception {
513 File source = File.createTempFile("source", null);
514 source.deleteOnExit();
515
516 File target = File.createTempFile("target", null);
517 target.deleteOnExit();
518
519 FileChannel fc1 = new FileOutputStream(source).getChannel();
520 FileChannel fc2 = new RandomAccessFile(target, "rw").getChannel();
521 try {
522 fc2.transferFrom(fc1, 0L, 0);
523 throw new RuntimeException("NonReadableChannelException expected");
524 } catch (NonReadableChannelException expected) {
525 } finally {
526 fc1.close();
527 fc2.close();
528 }
529 }
530
531
532
533
534 private static void initTestFile(File blah, long size) throws Exception {
535 if (blah.exists())
536 blah.delete();
537 FileOutputStream fos = new FileOutputStream(blah);
538 BufferedWriter awriter
539 = new BufferedWriter(new OutputStreamWriter(fos, "8859_1"));
540
541 for(int i=0; i<size; i++) {
542 awriter.write("e");
543 }
544 awriter.flush();
545 awriter.close();
546 }
547
548
549
550
551 static class EchoServer implements Runnable {
552 private SocketChannel sc;
553
554 public EchoServer(SocketChannel sc) {
555 this.sc = sc;
556 }
557
558 public void run() {
559 ByteBuffer bb = ByteBuffer.allocateDirect(1024);
560 try {
561 for (;;) {
562 int n = sc.read(bb);
563 if (n < 0)
564 break;
565
566 bb.flip();
567 while (bb.remaining() > 0) {
568 sc.write(bb);
569 }
570 bb.clear();
571 }
572 } catch (IOException x) {
573 x.printStackTrace();
574 } finally {
575 try {
576 sc.close();
577 } catch (IOException ignore) { }
578 }
579 }
580 }
581
582 }